<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="恢复成正式供应商" @ok="handleSubmit">
    <BasicForm @register="registerForm">
      <template #upload="{ model }">
        <Upload
          v-model:value="model.recoverFile"
          listType="dragger"
          :maxNumber="6"
          :accpet="fileAccpet"
          :tip="fileTip"
          :style="{ width: '200px' }"
        />
      </template>
    </BasicForm>
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { FormSchema } from '/@/components/Table';
  import Upload from '/@/components/Form/src/components/Upload.vue';
  import { regainSupplier } from '/@/api/erp/supplier/list';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  const FormSchema: FormSchema[] = [
    {
      field: 'number',
      label: '供应商编码',
      component: 'Input',
      colProps: { span: 24 },
    },
    {
      field: 'name',
      label: '供应商名称',
      component: 'Input',
      colProps: { span: 24 },
    },
    {
      field: 'recoverReason',
      label: '恢复理由',
      component: 'InputTextArea',
      required: true,
      colProps: { span: 24 },
    },
    {
      field: 'recoverFile',
      label: '文件上传',
      component: 'Input',
      slot: 'upload',
      colProps: { span: 24 },
    },
  ];

  const { notification } = useMessage();
  const rowId = ref('');
  const emit = defineEmits(['success', 'register']);
  const fileAccpet = '.doc,.docx,.xls,.xlsx,.jpg,.jpeg,.png,.pdf';
  const fileTip = '支持扩展名：.doc  .docx  .xls  .xlsx  .jpg  .jpeg  .png  .pdf;最多上传6个文件;';
  const [registerForm, { setFieldsValue, validate }] = useForm({
    labelWidth: 100,
    schemas: FormSchema,
    showActionButtonGroup: false,
    actionColOptions: {
      span: 23,
    },
  });

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    setModalProps({ confirmLoading: false, destroyOnClose: true });
    rowId.value = data.id;
    setFieldsValue({
      name: data.name,
      number: data.number,
    });
  });

  const handleSubmit = async () => {
    try {
      const values = await validate();
      setModalProps({ confirmLoading: true });
      values.id = rowId.value;
      await regainSupplier(values);
      notification.success({
        message: '恢复为正式供应商',
        description: t('成功'),
      });

      closeModal();
      emit('success');
    } catch (error) {
      setModalProps({ confirmLoading: false });
    }
  };
</script>
